package com.seabook.happy.website.servlets;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.seabook.happy.website.daos.BlogCommentDao;
import com.seabook.happy.website.pojos.BlogComment;

public class CommentUpdateServlet extends HttpServlet {
	private BlogCommentDao cDao = new BlogCommentDao();

	/**
	 * 
	 */
	private static final long serialVersionUID = 1296947910129432659L;
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String action = req.getParameter("action");
		if ("new".equals(action)) {
			postAComment(req, resp);
		} else if ("del".equals(action)) {
			
		} else if ("update".equals(action)) {
			
		}
	}
	
	private void postAComment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String username = req.getParameter("username");
		String email = req.getParameter("email");
		Text content = new Text(req.getParameter("comment"));
		String key = req.getParameter("article-id");
		BlogComment comment = new BlogComment();
		comment.setUsername(username);
		comment.setEmail(email);
		comment.setContent(content);
		comment.setModifiedDate(new Date());
		cDao.saveComment(comment, KeyFactory.stringToKey(key));
		
		resp.sendRedirect("showBlogArticle.jsp?id=" + key);
	}
}
